#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:chengyanan
@file: first_case.py
@time: 2019/12/21  3:41 下午
"""
import sys
import os
from log.user_log import UserLog

path = os.path.dirname(os.getcwd())
sys.path.append(path)
import unittest
from business.register_business import RegisterBusiness
from selenium import webdriver
import time
from case import HTMLTestRunner


class FirstCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.log = UserLog()
        cls.logger = cls.log.get_log()

    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("http://www.jd.com")
        self.logger.info("this is chrome")
        self.login = RegisterBusiness(self.driver)

    def test_login_username_null(self):
        print('test_login_username_null')
        name_null = self.login.login_user_null(name="", password="nannan")
        time.sleep(3)
        self.assertFalse(name_null)

    def test_login_password_error(self):
        print('test_login_password_error')
        password_null = self.login.login_password_null(name="", password="11")
        time.sleep(3)
        self.assertFalse(password_null, '执行成功')
        # time.sleep(3)
        # if password_null==True:
        #     print('登录成功,此条case失败')
        # else:
        #     print("此条case成功")

    def test_login_name_and_password_null(self):
        print("test_login_name_and_password_null")
        name_and_password_null = self.login.login_name_password_null(name="", password="")
        time.sleep(3)
        self.assertFalse(name_and_password_null)

    def test_login_success(self):
        print("test_login_success")
        success = self.login.user_base("onebadnan", "91799c")
        time.sleep(3)
        self.assertFalse(success)

    def tearDown(self):
        for method_name, error in self._outcome.errors:
            print('method_name', method_name)
            print('error', error)
            if error:
                case_name = self._testMethodName
                self.driver.save_screenshot("../report/" + case_name + ".png")

        self.driver.quit()

    @classmethod
    def tearDownClass(cls) -> None:
        cls.log.close_handle()


# def main():
#     first = FirstCase()
#     first.test_login_password_error()


if __name__ == '__main__':
    file_path = "../report/first_case.html"
    fp = open(file_path, "wb")
    suite = unittest.TestSuite()
    suite.addTest(FirstCase("test_login_username_null"))
    suite.addTest(FirstCase("test_login_password_error"))
    suite.addTest(FirstCase("test_login_name_and_password_null"))
    suite.addTest(FirstCase("test_login_success"))
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title="this is First report", description="京东报告", verbosity=2)
    runner.run(suite)
